<?php

namespace app\mp\controller;

use app\common\entity\BackResult;
use app\common\entity\Result;
use app\common\service\UserService;
use app\Request;
use app\wxapp\service\UserSignService;
use app\wxapp\service\WechatUserService;
use think\facade\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/9/29 0029
 * Time: 8:54
 */
class WechatUser extends Base
{

    public function update(Request $request)
    {
        $data = $request->post();
        $service = new WechatUserService();
        $model = $service->update($data, $this->user['id']);
        return Result::success($model);
    }

    public function updatePhone(Request $request)
    {
        $data = $request->post();
        $service = new WechatUserService();
        $user = $service->updatePhone($data, $this->user['id']);
        return Result::success($user);
    }

    /**
     * 获取不在用户表的个人数据
     */
    public function getExtra()
    {
        $userSignService = new UserSignService();
        $data = [
            'isSign' => $userSignService->isSignToday($this->user['id']),
            'signCountTotal' => $userSignService->signCountTotal($this->user['id']),
            'signCountMonth' => $userSignService->signCountMonth($this->user['id']),
        ];
        return Result::success($data);
    }

    /**
     * 签到
     */
    public function sign()
    {
        $service = new UserSignService();
        $service->sign($this->user['id']);
        return BackResult::success("签到成功");
    }

    public function getSignDays(Request $request)
    {
        $year = (int)$request->param('year', date('Y'));
        $month = (int)$request->param('month', date('m'));
        $service = new UserSignService();
        RETURN Result::success($service->signMonthList($this->user['id'], "{$year}-{$month}"));
    }

    /**
     * @param Request $request
     * @return \think\Response
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function remindStudy(Request $request)
    {
        $status = $request->param('status');
        $time = $request->param('time');
        $service = new WechatUserService();
        $service->remindStudy($this->user['id'], $status, $time);
        return $this->getRemindStatus();
    }

    /**
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRemindStatus()
    {
        $service = new WechatUserService();
        $status = $service->remindStatus($this->user['id']);
        return Result::success($status);
    }

    /**
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserInfo()
    {
        $user = Db::table('wechat_user')->where(['id' => $this->user['id']])->find();
        $user['vip'] = UserService::getVipInfo($this->user['id']);
        return Result::success($user);
    }
}